package com.xq.shop.controller;


import com.xq.shop.dto.EditColumnDto;
import com.xq.shop.dto.IntegerDto;
import com.xq.shop.dto.QueryDto;
import com.xq.shop.service.IColumnService;
import com.xq.shop.service.IUserService;
import com.xq.shop.vo.ColumnVo;
import com.xq.shop.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import utils.BasePage;
import utils.BaseResult;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 栏目表 前端控制器
 * </p>
 *
 * @author gaoxiaojin
 * @since 2024-07-22
 */
@RestController
@RequestMapping("/column")
public class ColumnController {

    private final IColumnService columnService;
    private final IUserService userService;

    @Autowired
    public ColumnController(IColumnService columnService, IUserService userService) {
        this.columnService = columnService;
        this.userService = userService;
    }

    @RequestMapping("/page")
    public BaseResult<BasePage<ColumnVo>> query(@RequestBody QueryDto dto, HttpServletRequest request) {
        userService.getInfo(request);
        BaseResult<BasePage<ColumnVo>> result = new BaseResult<>();
        BasePage<ColumnVo> page = columnService.pageQuery(dto);
        result.initializeSuccess(page);
        return result;
    }

    @RequestMapping("/edit")
    public BaseResult<Void> query(@RequestBody @Validated EditColumnDto dto, HttpServletRequest request) {
        UserVo vo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        columnService.editColumn(dto, vo.getId());
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/remove")
    public BaseResult<Void> remove(@RequestBody @Validated IntegerDto dto, HttpServletRequest request) {
        UserVo vo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        columnService.removeColumn(dto, vo.getId());
        result.initializeSuccess();
        return result;
    }
}
